using Flurl.Http;
using LibVLCSharp.Shared;
using LibVLCSharp.WinForms;
using M3U8Parser;

namespace VLC.Tests;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // Init();
        Parser();
    }

    public void Parser()
    {
        var url = new Uri("https://p.b8bf.com/video/shujuanyimeng/第15集/index.m3u8");

        var m3u8Content = url.GetStringAsync().Result;
        
        // 解析 M3U8 播放列表
        var masterPlaylist = MediaPlaylist.LoadFromText(m3u8Content);
        
        // EXT-X-MEDIA-SEQUENCE: 0
        // foreach(var stream in masterPlaylist.MediaSegments)
        // {
        //     Console.WriteLine("Uri:" + stream.Key );
        // }
    }


    public void Init()
    {
        var outputPath = "test-video";
        // 确保输出目录存在
        var outputDirectory = Path.Combine(Environment.CurrentDirectory, outputPath);
        if (!Directory.Exists(outputDirectory))
        {
            Directory.CreateDirectory(outputDirectory);
        }
        
        var url = new Uri("https://p.b8bf.com/video/shujuanyimeng/第15集/index.m3u8");
        var libVlc = new LibVLC(enableDebugLogs:true);
        var media = new Media(libVlc, url);
        
        // 设置输出文件
        media.AddOption($"sout=file/ts:{outputDirectory}/output.ts");
        media.AddOption("sout-keep"); // 保持连接直到完成
        
        // var media = new Media(libVlc,
        //     new Uri("https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8"));
        var mediaPlayer = new MediaPlayer(media);

        var videoView = new VideoView
        {
            MediaPlayer = mediaPlayer,
            Dock = DockStyle.Fill
        };

        this.Controls.Add(new Label
        {
            Text = "Video Player",
            Dock = DockStyle.Top,
            TextAlign = ContentAlignment.MiddleCenter,
            BackColor = Color.Black,
            ForeColor = Color.White
        });

        // 快进10秒按钮
        var fastForwardButton = new Button
        {
            Text = "快进10s",
            Dock = DockStyle.Bottom,
            BackColor = Color.LightGray,
            ForeColor = Color.Black,
        };
        fastForwardButton.Click += (sender, e) =>
        {
            // var newPosition = videoView.MediaPlayer.Position + (10.0f / videoView.MediaPlayer.Length);
            // if (newPosition > 1.0f) newPosition = 1.0f; // 确保不超过1.0
            // Console.WriteLine($"快进到: {newPosition}");
        };

        // 下载按钮
        var downloadButton = new Button
        {
            Text = "下载视频",
            Dock = DockStyle.Bottom,
            BackColor = Color.LightGray,
            ForeColor = Color.Black,
        };

        downloadButton.Click += (sender, e) =>
        {
            Console.WriteLine("下载视频按钮被点击");
        };
        this.Controls.Add(downloadButton);

        this.Controls.Add(fastForwardButton);

        this.Controls.Add(videoView);

        videoView.MediaPlayer.Play();

        videoView.MediaPlayer.PositionChanged += (sender, e) =>
        {
            Console.WriteLine($"Position: {e.Position}/{mediaPlayer.Length}");
            // 转换为 hh:mm:ss 时间
            var currentTime = TimeSpan.FromMilliseconds(mediaPlayer.Time);
            var totalTime = TimeSpan.FromMilliseconds(mediaPlayer.Length);
            var currentTimeString = $"{currentTime.Hours:D2}:{currentTime.Minutes:D2}:{currentTime.Seconds:D2}";
            var totalTimeString = $"{totalTime.Hours:D2}:{totalTime.Minutes:D2}:{totalTime.Seconds:D2}";
            Console.WriteLine($"当前时间: {currentTimeString} / 总时间: {totalTimeString}");
        };

        // Console.WriteLine("Video player initialized and playing.");
    }
}